import BtScroll from '@/components/scroll/new-scroll'
import NoData from '@/components/NoData/index'
import {initSelfWorkflow, setFavoriteWorkflow} from '@/api/loadWorkFlows'
import { ERR_OK } from '@/api/options/statusCode'
import DingtalkEnv from 'dingtalk-javascript-env'
import dingtalk from 'dingtalk-javascript-sdk'
import {getBaseUrl} from '@/utils/auth'
import { Toast } from 'mint-ui'
const InitWorkflowMixin = {
  data() {
    return {
      scrollY: 0,
      pullingUp: true,
      beforeScroll: true,
      probeType: 3,
      listenScroll: true,
      scrollbar: false, // 滚动条
      scrollbarFade: true,
      pullDownRefresh: true,
      pullDownRefreshThreshold: 90,
      pullDownRefreshStop: 40,
      pullUpLoad: true,
      pullUpLoadThreshold: 0,
      pullUpLoadMoreTxt: this.$i18n.t('scrollComponent.defaultLoadTxtMore'),
      pullUpLoadNoMoreTxt: this.$i18n.t('scrollComponent.defaultLoadTxtNoMore'),
      startY: 0,
      scrollToX: 0,
      scrollToY: -200,
      scrollToTime: 700,
      scrollToEasing: 'bounce',
      scrollToEasingOptions: ['bounce', 'swipe', 'swipeBounce'],
      activeClass: 'activeClass',
      activeClass2: 'activeClass2',
      activeClass3: 'activeClass3',
      showLoading: false,
      baseUrl: getBaseUrl()
    }
  },
  created() {
    this.probeType = 3
    this.listenScroll = true
    this.pullingUp = true
  },
  methods: {
    getDataList() {
      this.showLoading = true
      this.$store.dispatch('getWorkFlowLst')
        .then((res) => {
          this.showLoading = false
        })
        .catch((req) => {
          this.showLoading = false
        })
    },
    // 搜索
    handleSearch() {
      // console.log(this.searchList)
      let articlesArray = this.searchList
      let searchString = this.inputValue

      if (!searchString) {
        return articlesArray
      }

      articlesArray = articlesArray.filter(function(item) {
        if (item.DisplayName.toLowerCase().indexOf(searchString) !== -1) {
          return item
        }
      })

      this.$store.dispatch('setWorkFlowLst', articlesArray)
    },

    // 点击流程
    handleClickSelect(item, index) {
      this.showLoading = true
      this.initSelfWork(item)
    },

    // 设为常用
    setFavorite(item) {
      this.SetFavWork(item)
    },

    // 发起流程
    initSelfWork(data) {
      const options = {
        WorkflowCode: data.WorkflowCode,
        PageAction: 'Close',
        IsMobile: 'true'
      }
      return new Promise((resolve, reject) => {
        initSelfWorkflow(options).then(res => {
          this.showLoading = false
          if (res.code === ERR_OK) {
            const urls = this.baseUrl + res.data.url
            if (DingtalkEnv.isDingTalk && (DingtalkEnv.isWebAndroid || DingtalkEnv.isWebiOS || DingtalkEnv.platform == "iPad")) {
              dingtalk.ready(function() {
                const dd = dingtalk.apis
                dd.biz.util.openLink({
                  url: urls + `&loginfrom=dingtalk`,
                  onSuccess: function(result) {
                  },
                  onFail: function(err) {
                    console.log(err)
                  }
                })
              })
            } else {
              window.location.href = urls
            }
          }
          resolve(res)
        }).catch(error => {
          reject(error)
          this.loadingShow = false
        })
      })
    },

    // 设为常用
    SetFavWork(data) {
      const options = {
        workflowCode: data.WorkflowCode,
        isFavorite: !data.IsFavorite
      }
      return new Promise((resolve, reject) => {
        setFavoriteWorkflow(options).then(res => {
          this.showLoading = false
          if (res.code === ERR_OK) {
            let instance = Toast({
              message: this.$t('home.success'),
              iconClass: 'icon el-icon-success'
            })
            setTimeout(() => {
              instance.close()
              this.getDataList()
            }, 1000)
          } else {
            let instance = Toast({
              message: this.$t('home.fail'),
              iconClass: 'icon el-icon-error'
            })
            setTimeout(() => {
              instance.close()
              this.getDataList()
            }, 1000)
          }
          resolve(res)
        }).catch(error => {
          reject(error)
          this.loadingShow = false
        })
      })
    },

    // 下拉
    scroll(pos) {
      // console.log(pos.y)
    },

    onPullingDown() {
      setTimeout(() => {
        this.$refs.UserList.forceUpdate()
      }, 1000)
    },

    onPullingUp() {
      setTimeout(() => {
        this.$refs.UserList.forceUpdate()
      }, 1000)
    },
    //  刷新
    refresh() {
      this.$refs.UserList.refresh()
    },

    //  下拉监听
    listScroll() {
      this.$emit('listScroll')
    },
    // 点击类别
    categoryClick(target) {
      if (document.getElementById(target).style.display === '') {
        document.getElementById(target).style.display = 'none'
      } else {
        document.getElementById(target).style.display = ''
      }
    }
  },
  watch: {
    inputValue(value) {
      if (!value) {
        this.$store.commit('SER_SEARCH_LIST', this.searchList)
      }
    },
    scrollbarObj: {
      handler() {
        this.rebuildScroll()
      },
      deep: true
    },
    pullDownRefreshObj: {
      handler() {
        this.rebuildScroll()
      },
      deep: true
    },
    pullUpLoadObj: {
      handler() {
        this.rebuildScroll()
      },
      deep: true
    },
    startY() {
      this.rebuildScroll()
    }
  },
  computed: {
    workFlowLst() {
      return this.$store.getters.workFlowLst
    },
    workFlowLstIsFa() {
      const list = this.$store.getters.workFlowLst
      console.log(this.$store.getters.workFlowLst, 'workFlowLstIsFa')
      let arr = []
      list.forEach((item) => {
        if (item.Workflows) {
          item.Workflows.forEach((inner) => {
            if (inner.IsFavorite) {
              arr.push(inner)
            }
          })
        } else {
          if (item.IsFavorite) {
            arr.push(item)
          }
        }
      })
      let isFa = false
      if (arr.length > 0) {
        isFa = true
      }
      return isFa
    },
    scrollbarObj: function() {
      return this.scrollbar ? {fade: this.scrollbarFade} : false
    },
    pullDownRefreshObj: function() {
      return this.pullDownRefresh ? {
        threshold: parseInt(this.pullDownRefreshThreshold),
        stop: parseInt(this.pullDownRefreshStop)
      } : false
    },
    pullUpLoadObj: function() {
      return this.pullUpLoad ? {
        threshold: parseInt(this.pullUpLoadThreshold),
        txt: {more: this.pullUpLoadMoreTxt, noMore: this.pullUpLoadNoMoreTxt}
      } : false
    }
  },
  components: {
    BtScroll,
    NoData
  }
}
export default InitWorkflowMixin
